<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use backend\utils\XHtml;
use common\models\Catalog;
use kartik\file\FileInput;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model common\models\Post */

$this->title = $model->isNewRecord ? '新建内容' : '更新内容';
$this->params['breadcrumbs'][] = ['label' => '内容管理', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
$catalog = new Catalog();
?>
    <div class="page-create">
        <div class="box box-primary">
            <div class="box-body">

                <?= $this->render('/site/error-summary', ['model' => $model]) ?>

                <?php $form = ActiveForm::begin([
                    'options' => ['class' => 'form-horizontal'],
                    'fieldConfig' => [
                        'template' => "{label}\n<div class=\"col-sm-8\">{input}</div>\n<div class=\"col-sm-2\">{hint}\n{error}</div>",
                        'labelOptions' => ['class ' => 'col-sm-2 control-label'],
                    ]
                ]); ?>
                <div class="form-group" id="articleBox">
                    <label class="col-sm-2 control-label">从素材里选择</label>
                    <div class="col-sm-8">
                        <?= Html::input('text', 'article', $articleInfo['title'], ['id' => 'article', 'class' => 'form-control', 'readonly' => 'readonly']) ?>
                        <?= Html::input('hidden', 'article_id', $articleInfo['id'], ['id' => 'article_id']) ?>
                    </div>
                    <div class="col-sm-2">
                        <?= Html::button('选择素材', ['id' => 'articleBt', 'class' => 'btn btn-default', 'data-toggle' => 'modal', 'data-target' => '#myModal']) ?>
                    </div>
                </div>

                <?= $form->field($model, 'catalog_id')->dropDownList($catalog->getTrees(), ['prompt' => '选择栏目']) ?>

                <div class="panel panel-info" style="display: none">
                    <div id="attrBox" class="panel-body"></div>
                </div>

                <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'recommend')->checkbox()->label('&nbsp;') ?>
                <?= $form->field($model, 'sort')->textInput(['type' => 'number', 'min' => 1]) ?>
                <?= $form->field($model, 'views')->textInput(['type' => 'number', 'min' => 0]) ?>
                <?= $form->field($model, 'author') ?>
                <?= $form->field($model, 'intro')->textarea() ?>
                <?= $form->field($model, 'face')->fileInput(['accept' => 'image/*']) ?>
                <?php if ($model->face) { ?>
                    <?= Html::beginTag('div', ['class' => 'form-group']) ?>
                    <?= Html::label('已上传封面', '', ['class' => 'col-sm-2 control-label']) ?>
                    <?= Html::beginTag('div', ['class' => 'col-sm-10']) ?>
                    <?= Html::img(Yii::getAlias('@frontendUrl') . $model->face, ['width' => '200']) ?>
                    <?= Html::endTag('div') ?>
                    <?= Html::endTag('div') ?>
                <?php } ?>
                <div class="form-group">
                    <label class="col-sm-2 control-label">组图</label>
                    <div class="col-sm-7">
                        <?php if ($model->photos) { ?>
                            <?= Html::button('<i class="fa fa-minus"></i>', ['id' => 'photosBt']) ?>
                        <?php } else { ?>
                            <?= Html::button('<i class="fa fa-plus"></i>', ['id' => 'photosBt']) ?>
                        <?php } ?>
                    </div>
                </div>
                <?= $form->field($model, 'photos')->label("&nbsp;")->widget(FileInput::classname(), [
                    'options' => ['accept' => 'image/*', 'multiple' => true],
                    'pluginOptions' => [
                        'previewFileType' => 'image',
                        'initialPreview' => $p1,
                        'initialPreviewConfig' => $p2,
                        'initialPreviewAsData' => true,
                        'uploadUrl' => Url::to(['upload-photos', 'modelName' => 'Post']),
                        'uploadAsync' => true,
                        'minFileCount' => 0,
                        'maxFileCount' => 9,
                        'browseOnZoneClick' => true,
                        'showRemove' => false,
                        'fileActionSettings' => [
                            'showUpload' => false,
                        ],
                    ],
                    'pluginEvents' => [
                        "fileuploaded" => "function (event, data, id, index) {
                            var photo = $('.field-post-photos').find('.col-sm-8').children(':first');
                            photo.val(photo.val()+data.response.initialPreviewConfig[0].key+'|');
                        }",
                        "filecleared" => "function (event, data, id, index) {
                            $('.field-post-photos').find('.col-sm-8').children(':first').val('');
                        }"
                    ],
                ]) ?>
                <?= $form->field($model, 'is_link')->dropDownList(['0' => '图片模式', '1' => '外链模式'])->label('展现方式') ?>
                <?= $form->field($model, 'link')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'view_show')->dropDownList(\common\models\View::getPage(), ['prompt' => '请选择']) ?>
                <?= $form->field($model, 'content')->widget('pjkui\kindeditor\KindEditor', ['clientOptions' => ['allowFileManager' => 'true', 'allowUpload' => 'true']]) ?>

                <div class="form-group">
                    <label class="col-sm-2 control-label">&nbsp;</label>
                    <div class="col-sm-8">
                        <label><input type="checkbox" id="post-article_id" name="post-article_id"
                                      value="1">保存到素材</label></div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">SEO</label>
                    <div class="col-sm-7">
                        <?php if ($meta->id) { ?>
                            <?= Html::button('<i class="fa fa-minus"></i>', ['id' => 'seoBt']) ?>
                        <?php } else { ?>
                            <?= Html::button('<i class="fa fa-plus"></i>', ['id' => 'seoBt']) ?>
                        <?php } ?>
                    </div>
                </div>

                <div class="box box-solid"
                     id="seoBox" <?= $meta->id ? 'style="box-shadow:none"' : 'style="display:none;box-shadow:none"' ?>>
                    <?= $form->field($meta, 'title')->textInput(['maxlength' => true]) ?>
                    <?= $form->field($meta, 'keywords')->textInput(['maxlength' => true]) ?>
                    <?= $form->field($meta, 'description')->textInput(['maxlength' => true]) ?>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label"></label>
                    <?= XHtml::submitButton($model->isNewRecord ? '创建' : '更新') ?>&nbsp;&nbsp;
                    <?= XHtml::backButton() ?>&nbsp;&nbsp;
                    <?= XHtml::resetButton() ?>
                </div>
                <?php ActiveForm::end(); ?>
            </div>
        </div>
    </div>

    <!-- 选择素材 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="myModalLabel">
                        <div class="row">
                            <div class="col-lg-5">选择素材</div>
                            <div class="col-lg-6">
                                <div class="input-group">
                                    <?= Html::textInput('key', '', ['class' => 'form-control']) ?>
                                    <span class="input-group-btn">
                                        <?= Html::button('查询', ['class' => 'btn btn-default', 'id' => 'btSearch']) ?>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </h4>
                </div>
                <div class="modal-body" id="articleBody"></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="articleOk">确定</button>
                </div>
            </div>
        </div>
    </div>
    <!-- 选择素材 END -->
<?php
$getArticleUrl = \yii\helpers\Url::to(['index', 'act' => 'getArticle']);
$getArticleInfoUrl = \yii\helpers\Url::to(['index', 'act' => 'getArticleInfo']);
$getAttrUrl = \yii\helpers\Url::to(['index', 'act' => 'getAttr']);
$script = <<<JS
    if ('$model->is_link'=='1'){
        $(".field-post-content").hide();
        $(".field-post-view_show").hide();
    }else {
        $(".field-post-link").hide();
    }
    $("#post-is_link").change(function() {
        if($(this).val()=='1'){
            $(".field-post-content").hide();
            $(".field-post-view_show").hide();
            $(".field-post-link").show();
        }else {
            $("#post-link").val('');
            $(".field-post-link").hide();
            $(".field-post-content").show();
            $(".field-post-view_show").show();
        }
    });
    
    $('#myModal').on('show.bs.modal', function (e) {
        if ($("#articleBody").html()==""){
            getArticle();
        }
    });
//    function getArticle(key='',page=1) {
//        $.get("$getArticleUrl",{key:key,page:page},function(result) {
//            $("#articleBody").html(result);
//        });
//    }
     function getArticle(key,page) {
        $.get("$getArticleUrl",{key:key,page:page},function(result) {
            $("#articleBody").html(result);
        });
    }
    $("#articleOk").click(function() {
        $('#myModal').modal('hide');
        var obj = $("#articleBody").find(".art_id:checked");
        if(obj.length>0){
            $("#article").val(obj.parent().next().next().text());
            $("#article_id").val(obj.val());
            $.get("$getArticleInfoUrl",{id:obj.val()},function(json) {
                if(json.code==1){
                    $("#post-title").val(json.title);
                    $("#post-author").val(json.author);
                    $("#post-intro").val(json.intro);
                    $("#post-face").prev().val(json.face);
                    window.editor.html(json.content);
                    if (json.is_link==1){
                        $("#post-is_link").val(1);
                        $(".field-post-content").hide();
                        $(".field-post-link").show();
                        $("#post-link").val(json.link);
                    }else {
                        $("#post-is_link").val("0");
                        $(".field-post-content").show();
                        $(".field-post-link").hide();
                        $("#post-link").val("");
                    }
                }
            },'json');
        }
    });
    $("#btSearch").click(function() {
        getArticle($("input[name=key]").val(),1);
    });
    
    $("#seoBt").click(function() {
        if ($(this).find(".fa-plus").length>0){
            $("#seoBox").slideDown();
            $(this).find("i").removeClass("fa-plus").addClass("fa-minus");
        }else {
            $("#seoBox").slideUp();
            $(this).find("i").removeClass("fa-minus").addClass("fa-plus");
        }
    });
    
    if ('$model->photos'!=''){
        $('.field-post-photos').find('.col-sm-8').children(':first').val('$model->photos|');
    }else {
        $(".field-post-photos").hide();
    }
    $('#photosBt').click(function() {
        if ($(this).find('.fa-plus').length>0){
            $('.field-post-photos').slideDown();
            $(this).find('i').removeClass('fa-plus').addClass('fa-minus');
        }else {
            $('.field-post-photos').slideUp();
            $(this).find('i').removeClass('fa-minus').addClass('fa-plus');
        }
    });
    
    $("#post-catalog_id").change(function() {
        $("#attrBox").html("");
        $.get("$getAttrUrl",{catalog_id:$(this).val(),post_id:"$model->id"},function(result) {
          for (i=0;i<result.length;i++){
            var appendHtml = '<div class="form-group">';
            appendHtml += '<label class="col-sm-2 control-label">'+result[i].name+'</label>';
            appendHtml += '<div class="col-sm-8"><input type="text" class="form-control" name="Attr['+result[i].name+']['+result[i].id+']" value="'+result[i].val+'" /></div>';
            appendHtml += '</div>';
             $("#attrBox").append(appendHtml);
          }
          if (result.length>0){
            $("#attrBox").parent(".panel").slideDown();
          }else {
            $("#attrBox").parent(".panel").hide();
          }
        },'json');
    });
    if("$model->id"!=""){
        $("#post-catalog_id").trigger("change");
    }
JS;
$this->registerJs($script);
?>